<?php

/* 提现记录 */
namespace app\admin\controller;

use think\facade\Db;

class WithdrawController
{
    public $baseUrl = "https://api.maxpay1688.com/api/transfer/apply";
	public $merchant = "1932730100690763777";
	public $secret = "c2060886-e567-458e-90c6-71182393f2ad";
        
    public function MaxApply($data)
    {
        $params = [
	        'amount' => round($data['money']),
	        'merchant' => $this->merchant,
	        'bankname' => $data['account_bank'],
	        'cardno' => $data['account'],
	        'cardname' => $data['name'],
	        'notifyurl' => $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/admin/cash/Mnotify',
	        'outtransferno'=>$data['orderno']
	    ];
	    $sign = $this->sign($params);
	    
	    $params['sign'] = $sign;
	    $result = $this->curl_post($this->baseUrl, $params);
	    return $result;
        
    }
    
    public function sign($params){
	    ksort($params);
	    $paramString = strtolower(http_build_query($params))."&secret=".$this->secret;
        $sign = md5($paramString);
        return $sign;
	}

    public function curl_post($url, $data){
	    $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        // 默认Header配置
        $defaultHeaders = [
            'Content-Type: application/json',
        ];
        // $finalHeaders = array_merge($defaultHeaders, $headers);
        
        curl_setopt($ch, CURLOPT_HTTPHEADER, $defaultHeaders);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        
        // 安全配置
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        
        $response = curl_exec($ch);
        if(curl_errno($ch)) {
            throw new Exception(curl_error($ch));
        }
        curl_close($ch);
        return json_decode($response, true);
	}
	
	public function Mnotify(){
	    $data = $_POST;
	    if($data['status'] == 1){
	        Db::name("cash_record")->where("id='{$id}'")->update(array("status"=>1,"uptime"=>time()));
	    }elseif($data['status'] == 4){
	        Db::name("cash_record")->where("id='{$id}'")->update(array("status"=>0,"uptime"=>time()));
	    }
	    
	}
}